﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Text;

namespace Api.Dal
{
     public class Md5Tool
    {
        //对用户密码进行MD5的加密存储：使用MD5算法计算该字符串的哈希值，然后将哈希值转换为一个十六进制字符串。
        public static string EncryptPassword(string password)
        {
            using (MD5 md5Hash = MD5.Create())
            {
                byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(password));
                StringBuilder sBuilder = new StringBuilder();
                for (int i = 0; i < data.Length; i++)
                {
                    sBuilder.Append(data[i].ToString("x2"));
                }
                return sBuilder.ToString();
            }
        }

        //验证密码和加密后的密码是否一致
        public static bool ValidatePassword(string password, string storedHash)
        {
            string hash = EncryptPassword(password);
            return hash == storedHash; // 比较加密后的字符串与存储的哈希值
        }

    }
}
